package com.ultrasoft.farmasoft.repository;



import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Activesubstance;
import com.ultrasoft.farmasoft.domain.Typecomposition;
  
@Repository(value ="activeSubstanceDao")
@Transactional(readOnly = false)
public class ActiveSubstanceDaoImpl implements ActiveSubstanceDao {

	private EntityManager em = null;	
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	
	public Integer getSubstanceActiveId(){
		Integer res = 0;
		res = (Integer)em.createNamedQuery("Activesubstance.maxSubstanceActiveId").getSingleResult();
		if(res != null){
			res++;
		}else{
			res = 1;
		}
		return res;
	}
	
	@SuppressWarnings("unchecked")
	public List<Activesubstance> getActivesubstanceList() {
		return em.createNamedQuery("Activesubstance.findAll").getResultList();
	}

 /*
	//si llegara e tener un active ---- añadir active en la bd
	@SuppressWarnings("unchecked")
	public List<Typecomposition> getTypecompositionList(boolean active) {
		Query  q = em.createNamedQuery("Typecomposition.findByActive");
    	q.setParameter("active",active);
    	return q.getResultList();	
	}
*/

	public void add(Activesubstance activeSubstance) {
		em.persist(activeSubstance);
	}

	public void remove(Activesubstance activeSubstance) {
		try {
			em.remove(em.getReference(Activesubstance.class,activeSubstance.getActiveSubstanceId()));
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void update(Activesubstance activeSubstance) {
		em.merge(activeSubstance);
	}

	public Activesubstance findActiveSubstanceByID(Integer idActiveSubstance) {
		Activesubstance res=null;
		try {
	    	res=em.find(Activesubstance.class,idActiveSubstance);
		} catch (Exception e) {}
    	return res;
	}
}
